package com.chlitina.store.modules.cash.utils;

import java.util.Date;
import java.util.List;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

import com.chlitina.store.common.model.SatisfactionType;
import com.chlitina.store.common.utils.DateUtils;
import com.chlitina.store.common.utils.StringUtils;
import com.chlitina.store.modules.base.dao.StoreProductDao;
import com.chlitina.store.modules.base.entity.StoreProduct;
import com.chlitina.store.modules.cash.dao.StoreCardAllDao;
import com.chlitina.store.modules.cash.dao.StoreCardDurationDao;
import com.chlitina.store.modules.cash.dao.StoreCardProductDao;
import com.chlitina.store.modules.cash.dao.StoreCardTimesDao;
import com.chlitina.store.modules.cash.entity.StoreCardAll;
import com.chlitina.store.modules.cash.entity.StoreCardDuration;
import com.chlitina.store.modules.cash.entity.StoreCardProduct;
import com.chlitina.store.modules.cash.entity.StoreCardTimes;
import com.chlitina.store.modules.cash.entity.StoreConsume;
import com.chlitina.store.modules.cash.entity.StoreConsumeBuyProduct;
import com.chlitina.store.modules.cash.entity.StoreConsumeDoCare;
import com.chlitina.store.modules.cash.entity.StoreConsumeNewCard;
import com.chlitina.store.modules.cash.model.Card;
import com.chlitina.store.modules.cash.model.Member;
import com.chlitina.store.modules.cash.model.StoreProductCart;
import com.chlitina.store.modules.cash.service.StoreConsumeService;
import com.chlitina.store.modules.cash.web.ConsumeUtils;
import com.chlitina.store.modules.sys.utils.StoreUtils;

@Service
public class StoreCashUtils implements ApplicationContextAware{
	public static final int CONSUME_PRODUCT = 1;
	public static final int CONSUME_CARE = 2;
	public static final int CONSUME_NEW_CARD_OR_RECHANGE = 0;
	private static StoreCardAllDao storeCardAllDao;
	private static StoreCardProductDao storeCardProductDao;
	private static StoreCardDurationDao storeCardDurationDao;
	private static StoreCardTimesDao storeCardTimesDao;
	private static StoreProductDao storeProductDao;
	private static StoreConsumeService storeConsumeService;
	
	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		storeCardAllDao = (StoreCardAllDao) applicationContext.getBean("storeCardAllDao");
		storeCardProductDao = (StoreCardProductDao) applicationContext.getBean("storeCardProductDao");
		storeCardDurationDao = (StoreCardDurationDao) applicationContext.getBean("storeCardDurationDao");
		storeCardTimesDao = (StoreCardTimesDao) applicationContext.getBean("storeCardTimesDao");
		storeProductDao= (StoreProductDao) applicationContext.getBean("storeProductDao");
		storeConsumeService= (StoreConsumeService) applicationContext.getBean("storeConsumeService");
		
	}

	public static StoreConsume createStoreConstume(boolean isMemeber) {
		StoreConsume storeConsume = new StoreConsume();
		//storeConsume.setConfirmed(StoreConsume.TYPE_UNCONFIRMED);
		storeConsume.setConfirmed(StoreConsume.TYPE_CONFIRMED);
		storeConsume.setConsume_Time(new Date());
		storeConsume.setStore(StoreUtils.getStore());
		storeConsume.setConsume_Number(ConsumeUtils.generateConsumeNumber(storeConsume.getStore(),isMemeber));
		storeConsume
				.setSatisfaction(SatisfactionType.TYPE_SATISFACTION_UNKNOWN);
		storeConsume.setVerify_Code(StringUtils.generateVerifyCode());
		return storeConsume;
	}
	
	public static StoreProductCart find(List<StoreProductCart> list, long id) {
		for (StoreProductCart cart : list) {
			if (cart.getStoreProduct().getId().longValue() == id) {
				return cart;
			}
		}
       return null; 
	}
	
	public static boolean exist(List<StoreProductCart> list, long id) {
       return find(list,id)!=null; 
	}

	public static void add(List<StoreProduct> list, long id) {
		for (StoreProduct storeProduct : list) {
			if (storeProduct.getId().longValue() == id) {
				list.remove(storeProduct);
				break;
			}
		}
		StoreProduct storeProduct = storeProductDao.findOne(id);
		if (storeProduct != null) {
			list.add(storeProduct);
		}
	}

	public static void remove(List<StoreProduct> list, long id) {
		for (StoreProduct storeProduct : list) {
			if (storeProduct.getId().longValue() == id) {
				list.remove(storeProduct);
				break;
			}
		}
	}

	
	/**
	 * 
	 * @param member can be null
	 * @param spid
	 * @return
	 */
	public static long currentProductAmount(Member member, long spid ){
		StoreProduct spProduct = storeProductDao.findOne(spid);
		if(spProduct==null){
			return 0;
		}
		long currentAmount=spProduct.getAmount();
		if(currentAmount==0){
			return 0;
		}
		if(member==null){
			return currentAmount;
		}
		for (StoreProductCart cart : member.getShoppingCart().getSolutionProducts()) {
			if(cart.getStoreProduct().getId().longValue()==spid){
				currentAmount-=cart.getCount();
			}
		}
		
		for(StoreProductCart cart :member.getShoppingCart().getStoreProducts()){
			if(cart.getStoreProduct().getId().longValue()==spid){
				currentAmount-=cart.getCount();
			}
		}
		for(StoreProductCart cart :member.getShoppingCart().getSystemProducts()){
			if(cart.getStoreProduct().getId().longValue()==spid){
				currentAmount-=cart.getCount();
			}
		}
		for(StoreProductCart cart :member.getGiftCart().getStoreProducts()){
			if(cart.getStoreProduct().getId().longValue()==spid){
				currentAmount-=cart.getCount();
			}
		}
		
		for(StoreProductCart cart :member.getGiftCart().getSystemProducts()){
			if(cart.getStoreProduct().getId().longValue()==spid){
				currentAmount-=cart.getCount();
			}
		}
		return currentAmount;
	}
	
	
	public static Card getCard(int consume,int payType, Long id){
		Card card = new Card();
		//买产品
		if (consume ==  CONSUME_PRODUCT) {
			if (payType == StoreConsumeBuyProduct.PAY_TYPE_CARD_ALL) {
				StoreCardAll sca = storeCardAllDao.findOne(id);
				if (sca != null) {
					card.setTempleId(sca.getTemplate().getId());
					card.setName(sca.getTemplate().getName());
					card.setNumber(sca.getCardNumber());
					card.setRetail_price(sca.getTemplate().getRetail_price());
					card.setReal_price(sca.getMoney());
				}
			}
			if (payType == StoreConsumeBuyProduct.PAY_TYPE_CARD_PRODUCT) {
				StoreCardProduct scp = storeCardProductDao.findOne(id);
				if (scp != null) {
					card.setTempleId(scp.getTemplate().getId());
					card.setName(scp.getTemplate().getName());
					card.setNumber(scp.getCardNumber());
					card.setRetail_price(scp.getTemplate().getRetail_price());
					card.setReal_price(scp.getMoney());
				}
			}
		}
		//做护理
		if (consume ==  CONSUME_CARE) {
			if (payType == StoreConsumeDoCare.PAY_TYPE_All_CARD) {
				StoreCardAll sca = storeCardAllDao.findOne(id);
				if (sca !=null) {
					card.setTempleId(sca.getTemplate().getId());
					card.setName(sca.getTemplate().getName());
					card.setNumber(sca.getCardNumber());
					card.setRetail_price(sca.getTemplate().getRetail_price());
					card.setReal_price(sca.getMoney());
				}
			}
			if (payType == StoreConsumeDoCare.PAY_TYPE_TIMES_CARD) {
				StoreCardTimes sct = storeCardTimesDao.findOne(id);
				if (sct != null) {
					card.setTempleId(sct.getTemplate().getId());
					card.setName(sct.getTemplate().getName());
					card.setNumber(sct.getCardNumber());
					card.setRetail_price(sct.getTemplate().getRetail_price());
					card.setReal_price(sct.getRemainMoney());
				}
			}
			if (payType == StoreConsumeDoCare.PAY_TYPE_DURATION_CARD) {
				StoreCardDuration scd = storeCardDurationDao.findOne(id);
				if(scd !=null){
					card.setTempleId(scd.getTemplate().getId());
					card.setName(scd.getTemplate().getName());
					card.setNumber(scd.getCardNumber());
					card.setRetail_price(scd.getTemplate().getRetail_price());
					card.setReal_price(scd.getTemplate().getReal_cost());
				}
			}
			
		}
		//新开卡 和 充值
		if (consume == CONSUME_NEW_CARD_OR_RECHANGE) {
			if (payType == StoreConsumeNewCard.CARD_TYPE_ALL) {
				StoreCardAll sca = storeCardAllDao.findOne(id);
				if (sca != null) {
					card.setTempleId(sca.getTemplate().getId());
					card.setName(sca.getTemplate().getName());
					card.setNumber(sca.getCardNumber());
					card.setRetail_price(sca.getTemplate().getRetail_price());
					card.setReal_price(sca.getMoney());
				}
			}
			if (payType == StoreConsumeNewCard.CARD_TYPE_PRODUCT) {
				StoreCardProduct scp = storeCardProductDao.findOne(id);
				if (scp != null) {
					card.setTempleId(scp.getTemplate().getId());
					card.setName(scp.getTemplate().getName());
					card.setNumber(scp.getCardNumber());
					card.setRetail_price(scp.getTemplate().getRetail_price());
					card.setReal_price(scp.getMoney());
				}
			}
			if (payType == StoreConsumeNewCard.CARD_TYPE_TIMES) {
				StoreCardTimes sct = storeCardTimesDao.findOne(id);
				if (sct != null) {
					card.setTempleId(sct.getTemplate().getId());
					card.setName(sct.getTemplate().getName());
					card.setNumber(sct.getCardNumber());
					card.setRetail_price(sct.getTemplate().getRetail_price());
					card.setReal_price(sct.getRemainMoney());
				}
			}
			if (payType == StoreConsumeNewCard.CARD_TYPE_DURATION) {
				StoreCardDuration scd = storeCardDurationDao.findOne(id);
				if (scd != null) {
					card.setTempleId(scd.getTemplate().getId());
					card.setName(scd.getTemplate().getName());
					card.setNumber(scd.getCardNumber());
					card.setRetail_price(scd.getTemplate().getRetail_price());
					card.setReal_price(scd.getTemplate().getReal_cost());
				}
			}
		}
		return card;
	}
	
	public static int getTimesByCustomer(Long id, String start, String end){
		Date startDate = DateUtils.parseDate(start);
		Date endDate = DateUtils.parseDate(end);
		return storeConsumeService.findTimesByCustomer(id, startDate, endDate);
	}
	
	public static float getMoneyByCustomer(Long id, String start, String end){
		Date startDate = DateUtils.parseDate(start);
		Date endDate = DateUtils.parseDate(end);
		return storeConsumeService.findMoneyByCustomer(id, startDate, endDate);
	}
}
